# encoding=utf-8
# MVVM架构中的model，服务器原始数据解析器
# BingoLee 2024-05-07
import json
from model.kit_data import cache
from model.socket_client import SocketClient


class ServerDataParser(object):

    def __init__(self):
        self.all_data = {}

    def init_data(self) -> tuple:
        server_info = tuple(cache.get_settings_data('socket_server_info')[:2])
        result_data, err = SocketClient.send_message(server_info, "ALL_DATA")
        if result_data is None:
            return False, err
        self.all_data = json.loads(result_data)
        return True, None

    @classmethod
    def get_release_note(cls) -> tuple:
        server_info = tuple(cache.get_settings_data('socket_server_info')[:2])
        result_data, err = SocketClient.send_message(server_info, "RELEASE_NOTE")
        if result_data is None:
            return None, err
        return json.loads(result_data), None

    def get_key_value(self, key: str, default=None):
        return self.all_data.get(key, default)

    def get_release_version(self) -> tuple:
        if 'release_version' not in self.all_data:
            return None, "key `release_version` not found in server data"
        return self.all_data['release_version'], None

    def get_project_at_cmds(self) -> tuple:
        if 'project_at_cmds' not in self.all_data:
            return None, "key `project_at_cmds` not found in server data"
        return self.all_data['project_at_cmds'], None


server_data_parser = ServerDataParser()
